//
//  OrderViewCell.swift
//  EasyDana
//
//  Created by mac on 2024/10/17.
//

import UIKit
import Kingfisher
struct OrderModel: Codable {
    struct OrderListModel: Codable {
        let convoys: String//标题
        let afternoon: String//值
    }
    let hearts: Int//订单ID
    let elegant: String//产品名称
    let coffree: String//产品logo
    let tarescon: Int//订单状态
    let evenings: String//状态名称
    let tram: String//借款金额
    let kirk: String//跳转地址
    let teach: [OrderListModel]//订单列表显示数据
//    "joss": ""
    let joss: String
}

class OrderViewCell: UITableViewCell {
    @IBOutlet weak var loanAgreeButton: UIButton!
    @IBOutlet weak var amountLabel: UILabel!
    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var iconView: UIImageView!
    
    @IBOutlet weak var statusBGView: UIView!
    
    var jumpAction: ((OrderModel?)->Void)?
    
    var agreementAction: ((URL)->Void)?
    
    private lazy var stackView: UIStackView = {
        let s = UIStackView()
        s.axis = .vertical
        s.distribution = .fillEqually
        s.alignment = .fill
        s.spacing = 4
        return s
    }()
    
    @IBAction func loanAction(_ sender: UIButton) {
        if let url = self.model?.joss {
            if let u = URL(string: url) {
                UIViewController.top?.jump(url: u)
            }
        }
    }
    
    var _model: OrderModel?
    var model: OrderModel? {
        get {
            return _model
        }
        set {
            _model = newValue
            self.nameLabel.text = newValue?.elegant
            self.iconView.kf.setImage(with: URL(string: newValue?.coffree ?? ""))
            self.amountLabel.text = newValue?.tram
            self.statusLabel.text = newValue?.evenings
            print(newValue)
            let validAgreement = newValue?.joss.hasPrefix("http") ?? false
            self.loanAgreeButton.isHidden = !validAgreement
            let teach = newValue?.teach ?? []
            let views = teach.map({ model in
                let view = UIView()
                view.backgroundColor = UIColor.clear
                let titleLbl = UILabel()
                view.addSubview(titleLbl)
                titleLbl.text = model.convoys
                titleLbl.textColor = UIColor.hex(with: "#999999")
                titleLbl.font = UIFont.systemFont(ofSize: 14)
                titleLbl.snp.makeConstraints { make in
                    make.left.top.equalToSuperview()
                }
                let valueLbl = UILabel()
                view.addSubview(valueLbl)
                valueLbl.textColor = UIColor.hex(with: "#121011")
                valueLbl.font = UIFont.systemFont(ofSize: 14, weight: .medium)
                valueLbl.snp.makeConstraints { make in
                    make.left.equalTo(titleLbl.snp.right).offset(4)
                    make.centerY.equalTo(titleLbl)
                }
                valueLbl.text = model.afternoon
                return view
            })
            let oldViews = self.stackView.arrangedSubviews
            oldViews.forEach({$0.removeFromSuperview()})
            views.forEach { view in
                self.stackView.addArrangedSubview(view)
            }
            self.stackView.snp.remakeConstraints { make in
                make.left.equalTo(self.amountLabel)
                make.top.equalTo(self.amountLabel.snp.bottom).offset(12)
                make.right.equalToSuperview().offset(-15)
                make.height.equalTo(teach.count * 20 + (teach.count - 1) * 12)
            }
            
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.statusBGView.layer.cornerRadius = 13
        self.statusBGView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMaxYCorner]
//        self.contentView.addSubview(self.stackView)
        self.contentView.insertSubview(self.stackView, aboveSubview: self.loanAgreeButton)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
}
